#!/bin/bash

# Ruoyi-ai Application Stop Script

# 应用名称
APP_NAME="ruoyi-admin"
APP_HOME=$(cd "$(dirname "$0")" && pwd)
PID_FILE="$APP_HOME/app.pid"

# 检查PID文件是否存在
if [ ! -f "$PID_FILE" ]; then
  echo "PID file not found: $PID_FILE"
  echo "Application may not be running or was started without writing PID file."
  exit 1
fi

# 读取PID
PID=$(cat "$PID_FILE")

# 检查进程是否存在
if ps -p $PID > /dev/null; then
  echo "Stopping $APP_NAME (PID: $PID)..."
  kill $PID
  
  # 等待进程结束
  while ps -p $PID > /dev/null; do
    sleep 1
  done
  
  echo "$APP_NAME stopped successfully."
else
  echo "Process with PID $PID is not running."
fi

# 删除PID文件
rm -f "$PID_FILE"